package org.hit.burkun.calc;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

//import org.hit.burkun.db.DBHelper;
//import org.hit.burkun.db.DBTypeConverter;
//import org.hit.burkun.db.DBTypeConverter.DATATYPE;
import org.hit.burkun.model.Content;
import org.hit.burkun.model.UserProfile;
import org.hit.burkun.utils.StaticMethods;

public class VersionOne {
	/*
	 * 提取出简单用户和机器人用户，
	 * 把剩下的用户按照他们之前发微博的编辑距离相似度给予值
	 * 机器人用户根据平均的值赋值
	 * predict_in_train_robot_user
	 * predict_in_train_simple_user
	 * predict_in_train_other_user
	 * predict_not_in_train 全部置0
	 */
	public static void main(String[] args) {
		//other_user
		//设定counter，和最大比较次数
		
		int maxCounter = 666;
		for(String uid : other_user){
			System.out.println(uid);
			Collection<String> pmids = StaticMethods.getUserPredictMid(uid);
			Collection<String> tmids = StaticMethods.getUserTrainMid(uid);
			for(String pmid : pmids){
				String[] line = new String[5];
				int counter = 0;
				double maxScore = 0;
				String[] scores = null;
				for(String tmid: tmids){
					counter++;
					Content pclean = resPredict.get(pmid);
					Content tclean = resTrain.get(tmid);
					if(pclean != null && tclean !=null){
						double s = Content.getEditSim(pclean, tclean);
						if(s > maxScore){
							//转发 评论 赞
							String[] ss = midThreeNumber.get(tmid);
							if(ss !=null) {maxScore = s; scores = ss;} ;
						}
					}
					if(counter > maxCounter || maxScore > 0.9) break;
				}
				line[0] = uid;
				line[1] = pmid;
				if(scores!=null){
					line[2] = scores[1];
					line[3] = scores[2];
					line[4] = scores[3];
				}else{
					line[2] = "0";
					line[3] = "0";
					line[4] = "0";
				}
				insertData.add(line);
			}
		}
		insertDataToFile(insertData);
		
		//complex user
		int maxCounter2 = 666;
		for(String uid : complex_user){
			System.out.println(uid);
			Collection<String> pmids = StaticMethods.getUserPredictMid(uid);
			Collection<String> tmids = StaticMethods.getUserTrainMid(uid);
			for(String pmid : pmids){
				String[] line = new String[5];
				int counter = 0;
				double maxScore = 0;
				String[] scores = null;
				for(String tmid: tmids){
					counter++;
					Content pclean = resPredict.get(pmid);
					Content tclean = resTrain.get(tmid);
					if(pclean != null && tclean !=null){
						double s = Content.getEditSim(pclean, tclean);
						if(s > maxScore){
							String[] ss = midThreeNumber.get(tmid);
							if(ss !=null) {maxScore = s; scores = ss;} ;
						}
					}
					if(counter > maxCounter2 || maxScore > 0.9) break;
				}
				line[0] = uid;
				line[1] = pmid;
				if(scores!=null){
					line[2] = scores[1];
					line[3] = scores[2];
					line[4] = scores[3];
				}else{
					line[2] = "0";
					line[3] = "0";
					line[4] = "0";
				}
				insertData.add(line);
			}
		}
		insertDataToFile(insertData);
		
		//simple_user， not_in_user
		for(String uid : simple_user){
			Collection<String> mids = StaticMethods.getUserPredictMid(uid);
			for(String mid : mids){
				String[] line = new String[5];
				line[0] = uid;
				line[1] = mid;
				line[2] = "0";
				line[3] = "0";
				line[4] = "0";
				insertData.add(line);
			}
		}
		insertDataToFile(insertData);
		for(String uid : not_in_user){
			Collection<String> mids = StaticMethods.getUserPredictMid(uid);
			for(String mid : mids){
				String[] line = new String[5];
				line[0] = uid;
				line[1] = mid;
				line[2] = "0";
				line[3] = "0";
				line[4] = "0";
				insertData.add(line);
			}
		}
		insertDataToFile(insertData);
		//robot_user
		for(String uid : robot_user){
			UserProfile up = StaticMethods.getUserProfile(uid);
			long likes = Math.round(up.likeSum / (double)up.days);
			long repost = Math.round(up.repostSum / (double)up.days);
			long comment =  Math.round(up.commentSum/ (double)up.days);
			Collection<String> mids = StaticMethods.getUserPredictMid(uid);
			for(String mid : mids){
				String[] line = new String[5];
				line[0] = uid;
				line[1] = mid;
				line[2] = String.valueOf(repost);
				line[3] = String.valueOf(comment);
				line[4] = String.valueOf(likes);
				insertData.add(line);
			}
	/*		System.out.println(uid);
			Collection<String> pmids = StaticMethods.getUserPredictMid(uid);
			Collection<String> tmids = StaticMethods.getUserTrainMid(uid);
			for(String pmid : pmids){
				String[] line = new String[5];
				int counter = 0;
				double maxScore = 0;
				String[] scores = null;
				for(String tmid: tmids){
					counter++;
					Content pclean = resPredict.get(pmid);
					Content tclean = resTrain.get(tmid);
					if(pclean != null && tclean !=null){
						double s = Content.getEditSim(pclean, tclean);
						if(s > maxScore){
							String[] ss = midThreeNumber.get(tmid);
							if(ss !=null) {maxScore = s; scores = ss;} ;
						}
					}
					if(counter > maxCounter2 || maxScore > 0.9) break;
				}
				line[0] = uid;
				line[1] = pmid;
				if(scores!=null){
					line[2] = scores[1];
					line[3] = scores[2];
					line[4] = scores[3];
				}else{
					line[2] = "0";
					line[3] = "0";
					line[4] = "0";
				}
				insertData.add(line);
			}
			*/
		}
		insertDataToFile(insertData);
		
	}
	
	
	private static void insertDataToFile(Collection<String[]> res){
//		DBHelper dbh = DBHelper.getServerDataDBHelper();
//		dbh.connectionDB();
//		DATATYPE[] types= new DATATYPE[5];
//		types[2] = DATATYPE.I;
//		types[3] = DATATYPE.I;
//		types[4] = DATATYPE.I;
//		DBTypeConverter tc = new DBTypeConverter(types);
//		dbh.insertBatch("version_1", res, tc);
//		dbh.disconnection();
		//转发 评论 赞
		StaticMethods.writeFile(res);
		res.clear();
	}
	
	private static Collection<String[]> insertData = new LinkedList<>();
	private static Map<String, String[]> midThreeNumber = StaticMethods.getMidThreeNumber();
	private static Map<String, Content> resPredict = StaticMethods.getMid2ContentPredict();
	private static Map<String, Content> resTrain = StaticMethods.getMid2ContentTrain();
	private static Collection<String> other_user = StaticMethods.getUids("predict_in_train_other_user");
	private static Collection<String> complex_user = StaticMethods.getUids("predict_in_train_complex_user");
	
	
	private static Collection<String> robot_user = StaticMethods.getUids("predict_in_train_robot_user");
	private static Collection<String> simple_user = StaticMethods.getUids("predict_in_train_simple_user");
	private static Collection<String> not_in_user = StaticMethods.getUids("predict_not_in_train");
	
}
